using System;

namespace TSharp.Core.Util.TypeConvert
{
    /// <summary>
    /// 
    /// </summary>
    /// <author>
    /// tangjingbo
    /// </author>
    /// <remarks>
    /// tangj15 at 2012-3-16 9:59
    /// </remarks>
    public class NullableTimeSpanTypeDetectorImpl : NullableValueTypeDetector<TimeSpan>
    {
        /// <summary>
        /// Strings the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override TimeSpan? String(string detectorObj, TimeSpan? arg)
        {
            TimeSpan result;
            bool parseSucceed = System.TimeSpan.TryParse(detectorObj, out result);
            return parseSucceed ? result : arg;
        }

        /// <summary>
        /// Times the span.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override TimeSpan? TimeSpan(TimeSpan detectorObj, TimeSpan? arg)
        {
            return detectorObj;
        }

        /// <summary>
        /// Int64s the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override TimeSpan? Int64(long detectorObj, TimeSpan? arg)
        {
            return System.TimeSpan.FromTicks(detectorObj);
        }

        /// <summary>
        /// Int32s the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override TimeSpan? Int32(int detectorObj, TimeSpan? arg)
        {
            return System.TimeSpan.FromMilliseconds(detectorObj);
        }

        /// <summary>
        /// Int16s the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override TimeSpan? Int16(short detectorObj, TimeSpan? arg)
        {
            return System.TimeSpan.FromMilliseconds(detectorObj);
        }

        /// <summary>
        /// Doubles the specified detector obj.
        /// </summary>
        /// <param name="detectorObj">The detector obj.</param>
        /// <param name="arg">The arg.</param>
        /// <returns></returns>
        public override TimeSpan? Double(double detectorObj, TimeSpan? arg)
        {
            return System.TimeSpan.FromMilliseconds(detectorObj);
        }
    }
}